package com.szholly.pro.watergas_server.ps.android.controller;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

/**
 * 排水管网查询分页请求
 *
 */
public class PipelinesPagingRequest {
	private String pipelinesType;
	private String searchText;
	private String unit;
	private String street;
	private int count;
	private int pageSize;
	private int pageCount;
	private int pageIndex;

	public PipelinesPagingRequest() {
	}

	public static PipelinesPagingRequest getPipelinesPagingRequest(
			String request) {
		PipelinesPagingRequest pipelinesPagingRequest = new PipelinesPagingRequest();
		try {
			JSONObject jsonObject = JSONObject.fromObject(request);
			pipelinesPagingRequest.searchText = jsonObject
					.optString("searchText", "");
			pipelinesPagingRequest.pipelinesType = jsonObject
					.optString("pipelinesType", "");
			pipelinesPagingRequest.unit = jsonObject.optString("unit", "");
			pipelinesPagingRequest.street = jsonObject.optString("street", "");
			pipelinesPagingRequest.pageIndex = jsonObject.optInt("pageIndex",
					1);
			pipelinesPagingRequest.pageSize = jsonObject.optInt("pageSize", 10);
		} catch (JSONException e) {
			pipelinesPagingRequest.pageSize = 10;
			pipelinesPagingRequest.pageIndex = 1;
		}
		if (pipelinesPagingRequest.pageIndex <= 0) {
			pipelinesPagingRequest.pageIndex = 1;
		}
		if (pipelinesPagingRequest.pageSize <= 0) {
			pipelinesPagingRequest.pageSize = 10;
		}
		return pipelinesPagingRequest;
	}

	public String getPipelinesType() {
		return pipelinesType;
	}

	public void setPipelinesType(String pipelinesType) {
		this.pipelinesType = pipelinesType;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public void calculatePaging() {
		if (pageSize != 0) {
			pageCount = (count + pageSize - 1) / pageSize;
		} else {
			pageCount = 0;
		}
	}

	public JSONObject toJSONObect() {
		JSONObject resultJson = new JSONObject();
		resultJson.put("pageCount", getPageCount());
		resultJson.put("pageIndex", getPageIndex());
		resultJson.put("pageSize", getPageSize());
		resultJson.put("rowCount", getCount());
		resultJson.put("refreshRowCount", true);
		return resultJson;
	}
}
